//单链表中是否存在环
#include <stdio.h>
#include "list_node.h"
//是否存在环 快慢指针法
static int hasCycle(struct ListNode *head)
{
    if (head == NULL)
    {
        return 0;
    }
    struct ListNode *slow = head;
    struct ListNode *fast = head->next;
    while (fast && slow)
    {
        fast = fast->next;
        if (fast == NULL)
        {
            break;
        }
        fast = fast->next;
        slow = slow->next;
        if (slow == fast)
        {
            return 1;
        }
    }

    return 0;
}
int main_15()
{
    struct ListNode node1 = {3, NULL};
    struct ListNode node2 = {2, NULL};
    struct ListNode node3 = {0, NULL};
    struct ListNode node4 = {-4, NULL};

    node1.next = &node2;
    node2.next = &node3;
    node3.next = &node4;
    node4.next = &node2;

    int r = hasCycle(&node1);
    printf("%d\n", r);
}